<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language
//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: DescriptionNews
//Chức năng	: Show ra mô tả của tin tức
 
class DescriptionThing_IndexController {

	public function init(){
		parent::init();
	}

	//Action index thục hiện chức năng show thông tin của module
	public function indexAction(){
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/descriptionthing');
		$tbLanguage = new Language_Model_Language();
		$this->view->languages = $tbLanguage->getAllLanguages();
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
			$this->view->language = $name['name'];
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$this->view->question		= $language->get('text_question');
		$this->view->by				= $language->get('text_by');
		$this->view->date			= $language->get('text_date');
		$this->view->pq				= $language->get('text_pq');
		$this->view->textConfirm	= $language->get('text_confirm');
		/*========== End Get Text ==============*/
		
		//Start------Khai báo các parameter
		$tbThings= new DescriptionThing_Model_Things();
		$tbLike= new DescriptionThing_Model_Like();		
		$dateInt = new Util();
		$access=false;
		//End--------Khai báo các parameter
		
		//Start------Check Input Parameter
		if(isset($_GET["id"]) && $dateInt->is_interger($_GET["id"]))
		{
			//Check thing_id có trong db?
			$DescriptionNews = $tbThings->getThingById($_GET["id"]);
			$i = 0;
			foreach($DescriptionNews as $News)
			{
				$title_lang=unserialize($News["title"]);
				if(isset($title_lang[$name['name']]))
					$DescriptionNews[$i]["title"]=$title_lang[$name['name']];
				else
					$DescriptionNews[$i]["title"]="------------------";
				
				$description_lang = unserialize($News["description"]);
				if (isset($description_lang[$name['name']]))
					$DescriptionNews[$i]["description"] = $description_lang[$name['name']];
				else
					$DescriptionNews[$i]["description"] = "...";
					$i++;
			}
			if(count($DescriptionNews)>0)
				$access=true;
			$this->view->Thing_id = $_GET['id'];			
		}
		if(!$access) 
		{
			$this->_redirect('/index/index');
		}
		//End--------Check Input Parameter
				
		
		//Đếm số người thích tin này
		$CountLike=count($tbLike->getLikeByThingsId($_GET["id"]));
			
		//Start--------Lấy ngày đăng tin
		$Update_Date=$dateInt->makeDays($DescriptionNews[0]['update_date']); 
		//End--------Lấy ngày đăng tin

		//-----------------------Start update view of things------------------------
		$DescriptionNews[0]['views']=$DescriptionNews[0]['views']+1;
		$tbThings->updateViewThing($_GET["id"],$DescriptionNews[0]['views']);
		//-----------------------end update view of things------------------------//
		
			$session = new Zend_Session_Namespace('login.auth');
    	if(isset($session->id_user))
		{
			$this->view->login = "1";
			$this->view->user_name = $session->name_user;
			/************ Start check permission *********/
			$path = APPLICATION_PATH . '/widgets/descriptionthing/';
			$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
			$user = new User();
			$user->init();
			$user->check_login();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		
		//Start------Truyền parameter qua View
		$titleXML = 'title'.$name['name'];
		if(isset($xmlconfig->$titleXML))
			$this->view->title = $xmlconfig->$titleXML;
		else
        	$this->view->title = $xmlconfig->titleDefault;
		$this->view->Update_Date=$Update_Date;				
		$this->view->DescriptionNews = $DescriptionNews;
		$this->view->CountLike = $CountLike;
					
		//End--------Truyền parameter qua View										
	}		

}